import tensorflow as tf
import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv('../data/Credit.csv', header=None)

# 查看第15列结果有几类
print(data.iloc[:, -1].value_counts())

# 构造x,y
x = data.iloc[:, :-1]
y = data.iloc[:, -1].replace(-1, 0)

# model = tf.keras.Sequential()
# model.add(tf.keras.layers.Dense(10, input_shape=(15,), activation='relu'))
# model.add(tf.keras.layers.Dense(10, activation='relu'))
# model.add(tf.keras.layers.Dense(1, activation='sigmoid'))

model = tf.keras.Sequential([
    tf.keras.layers.Dense(10, input_shape=(15,), activation='relu'),
    tf.keras.layers.Dense(10, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

model.summary()

# 设置优化器、损失函数，binary_crossentropy主要指结果为01两种
model.compile(
    optimizer='adam',  # 优化器
    loss='binary_crossentropy',  # 损失函数，交叉熵
    metrics=['acc']  # 准确率
)

# 训练80次
history = model.fit(x, y, epochs=100)

print(history.history.keys())

# 绘制训练次数与loss图像
plt.plot(history.epoch, history.history.get('loss'))
plt.show()

# 绘制训练次数与精确度图像
plt.plot(history.epoch, history.history.get('acc'))
plt.show()

print(model.predict(x))



